//
//  ZZHomeTextListVC.swift
//  QMXK
//
//  Created by jenkin on 2018/12/4.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit
import ZHRefresh

class ZZHomeTextListVC: ZZBaseTableVC {

    let textCellID = "textCellID"
    let titleImageCellID = "titleImageCellID"
    let leftTitleImageCellID = "leftTitleImageCellID"
    var artircleModel:ZZArticleTypeModel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUI()
        self.loadData()
        self.tableView.header?.beginRefreshing()
    }
    
    override func loadData(){
        super.loadData()
        self.data = [];
        self.tableView.reloadData()
    }
    
    override func setUI() {
        super.setUI()
        self.tableView.register(ZZHomeTextTitleCell.classForCoder(), forCellReuseIdentifier: textCellID)
        self.tableView.register(ZZHomeTextTitleAndImageCell.classForCoder(), forCellReuseIdentifier: titleImageCellID)
        self.tableView.register(ZZHomeTextLeftTitleAndImageCell.classForCoder(), forCellReuseIdentifier: leftTitleImageCellID)
    }
    
    override func headerRefresh() {
        var param = [String:Any]()
        param["page"] = self.page
        param["type"] = self.artircleModel.id
        //ZZArticleListModel
        weak var weakSelf = self
        ZZHttpRequest.shareRequest.request(target: MoyaApi.articleListType(data: param), successBlock: { (data:Dictionary) in
            let articleListModel:ZZArticleListModel! = ZZArticleListModel.deserialize(from: data)
            weakSelf?.data.removeAll()
            weakSelf?.data = (articleListModel.data?.article_list as! [AnyObject]) + (articleListModel.data!.video_list as! [AnyObject])
            weakSelf?.tableView.reloadData()
            weakSelf?.tableView.header?.endRefreshing()
        }) { (error) in
            weakSelf?.tableView.header?.endRefreshing()
        }
    }
    
    override func footerRefresh() {
        self.tableView.footer?.endRefreshing()
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let model:AnyObject = self.data[indexPath.row]
        //文章
        if model is ZZArticleModel{
            let articleModel:ZZArticleModel? = model as? ZZArticleModel
//            if articleModel?.special == 0{
//                let cell:ZZHomeTextTitleCell = tableView.dequeueReusableCell(withIdentifier: textCellID, for: indexPath) as! ZZHomeTextTitleCell
//                cell.model = articleModel
//                return cell
//            }else
            if articleModel?.special == 0{
                let cell:ZZHomeTextTitleAndImageCell = tableView.dequeueReusableCell(withIdentifier: titleImageCellID, for: indexPath) as! ZZHomeTextTitleAndImageCell
                cell.model = articleModel
                return cell
            }else{
                let cell:ZZHomeTextLeftTitleAndImageCell = tableView.dequeueReusableCell(withIdentifier: self.leftTitleImageCellID, for: indexPath) as! ZZHomeTextLeftTitleAndImageCell
                cell.model = articleModel
                return cell
            }
            
        }
        //视频
        else{
            let videoModel:ZZVideoModel? = model as? ZZVideoModel
        }
        
        let cell:ZZHomeTextLeftTitleAndImageCell = tableView.dequeueReusableCell(withIdentifier: self.leftTitleImageCellID, for: indexPath) as! ZZHomeTextLeftTitleAndImageCell
        return cell
    }
}
